package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.dialog.WonderLoadingDialog;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerOperatingDataFragmentComponent;
import com.wgts.wgts.dagger.module.OperatingDataFragmentModule;
import com.wgts.wgts.mvp.presenter.IOperatingDataFragmentPresenter;
import com.wgts.wgts.mvp.view.IOperatingDataFragmentView;
import com.wgts.wgts.netmodle.resp.OperatingDataEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class OperatingDataFragment extends LazyBaseFragment implements IOperatingDataFragmentView {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;

    private OperatingDataAdapter adapter;

    public static final String RETURN_GOOD = "RETURN_GOOD";

    public static final String ON_GOING = "ON_GOING";

    public static final String COMPLETED = "COMPLETED";

    public static final String TYPE = "type";

    private ArrayList<OperatingDataEntity.Entity> srcDate;

    private WonderLoadingDialog dialog;

    private String type;

    @Override
    protected void loadData() {


        type = getArguments().getString(TYPE);

        showDialog();
        presenter.initOperatingDate("", type);

    }

    @Inject
    IOperatingDataFragmentPresenter presenter;

    @Override
    public void init() {
        DaggerOperatingDataFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .operatingDataFragmentModule(new OperatingDataFragmentModule(this))
                .build()
                .inject(this);
        srcDate = new ArrayList<>();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.asset_detail_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {
        dialog = new WonderLoadingDialog(getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new OperatingDataAdapter(getContext(), srcDate);
        recycleView.setAdapter(adapter);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.moreOpertingDate();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.initOperatingDate("", type);
            }
        });
    }


    @Override
    public void onError(int status, String msg) {
        dismissDialog();
    }

    public static Fragment getInstance(String type) {
        Fragment operatingDataFragment = new OperatingDataFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TYPE, type);
        operatingDataFragment.setArguments(bundle);
        return operatingDataFragment;
    }

    @Override
    public void moreOperatingDateResult(boolean noMore, OperatingDataEntity req) {
        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }else{
            refreshLayout.setEnableLoadmore(true);
        }

        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        srcDate.addAll(req.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initOperatingDateResult(boolean noMore, OperatingDataEntity req) {
        dismissDialog();
        if(req.list.size() == 0){
            noneDataLy.setVisibility(View.VISIBLE);
        }else{
            noneDataLy.setVisibility(View.GONE);
        }

        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }else{
            refreshLayout.setEnableLoadmore(true);
        }

        refreshLayout.finishRefresh();
        adapter.setNoMore(noMore);
        srcDate.clear();
        srcDate.addAll(req.list);
        adapter.notifyDataSetChanged();

    }

    private void showDialog(){
        if (dialog != null){
            dialog.show();
        }
    }

    private void dismissDialog(){
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }
}
